<template>
  <div class="app-container">
    <!-- 补贴单信息 -->
    <h5 class="titleInfo">补贴单信息</h5>
    <el-form ref="list" :inline="true" :model="list" label-width="150px">
      <el-row>
        <el-col :span="6">
          <el-form-item label="补贴单号">
            <span class="spanToInput">{{list.applySn | filterNull}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="医院名称">
            <span class="spanToInput">{{list.hospitalName | filterNull}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="项目名称">
            <span class="spanToInput">{{list.projectName | filterNull}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="报单时间">
            <span class="spanToInput">{{list.createdAt | filterNull}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="付款方式">
            <span class="spanToInput">{{list.payType}}</span>
          </el-form-item>
        </el-col>
        <el-row>
          <el-col :span="8">
            <el-form-item label="报单类型">
              <span class="spanToInput">{{list.formType | filterNull}}</span>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="分成模式">
              <span class="spanToInput">{{list.separateType | filterNull}}</span>
            </el-form-item>
          </el-col>
        </el-row>

        <el-col :span="8">
          <el-form-item label="消费金额">
            <span class="spanToInput">{{list.money | filterNull}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="退款金额">
            <span class="spanToInput">{{refundList.refundMoney | filterNull}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="剩余金额(消费金额)">
            <span class="spanToInput">{{refundList.money | filterNull}}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="扣除杂费金额">
            <span class="spanToInput">{{(list.allowanceMoney-0)+(list.useCoin-0) | filterNull}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="退款金额">
            <span class="spanToInput">{{refundList.refundAllowanceMoney | filterNull}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="剩余金额(补贴金额)">
            <span class="spanToInput">{{refundList.allowanceMoney | filterNull}}</span>
          </el-form-item>
        </el-col>

        <el-col :span="8">
          <el-form-item label="杂费金额">
            <span class="spanToInput">{{(list.money-0) - (list.allowanceMoney-0)-(list.useCoin-0) }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="退款金额">
            <span class="spanToInput">{{refundList.refundFees | filterNull}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="剩余金额">
            <span class="spanToInput">{{refundList.leftFees | filterNull}}</span>
          </el-form-item>
        </el-col>
        <!-- s19 -->
        <el-col :span="8">
          <el-form-item label="实际扣除现金金额">
            <span class="spanToInput">{{list.allowanceMoney | filterNull}}</span>
          </el-form-item>
        </el-col>
        <el-col :span="8">
          <el-form-item label="实际扣除金币">
            <span class="spanToInput">{{list.useCoin | filterNull}}</span>
          </el-form-item>
        </el-col>
        <!-- s19 -->

        <el-col :span="8">
          <el-form-item label="2020活动退款" v-if="list.is2020==1"> 
            <span class="spanToInput">
              <el-switch
                v-model="list.is2020Refundval"
                :active-value="1"
                :inactive-value="0"
                :disabled="list.is2020Refund==1"
                active-color="#13ce66"
                inactive-color="#eee"
              ></el-switch>
            </span>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="list.payTypeInt !==4">
          <el-form-item label="退款扣除杂费金额">
            <el-input v-model="refundAllMoney" placeholder="请输入退款扣除杂费金额"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12" v-if="list.payTypeInt !==4">
          <el-form-item label="退款杂费金额">
            <el-input v-model="feeAmount" placeholder="请输入退款杂费金额"></el-input>
          </el-form-item>
          <el-button type="primary" @click="sumpay()" plain>计算</el-button>
        </el-col>
        <el-col :span="24" v-if="list.payTypeInt==4">
          <p style="text-align:right;">
            <el-button type="primary" @click="sumpay">计算</el-button>
          </p>
          <el-table
            :data="list.payDetailsList"
            border
            class="tableData"
            :summary-method="getSummaries"
            show-summary
            style="width: 100%; margin-top: 20px"
          >
            <el-table-column prop="payTypeStr" label="付款方式" width="180"></el-table-column>
            <el-table-column prop="payTotalAmount" label="流水单消费总金额"></el-table-column>
            <el-table-column prop="payAllAmount" label="流水单奖励金额"></el-table-column>
            <el-table-column prop="payFeeAmount" label="流水单杂费金额"></el-table-column>
            <el-table-column prop="AllMoney" label="补贴单退款奖励金额">
              <span slot-scope="scope">
                <el-input
                  v-model="scope.row.AllMoney"
                  :disabled="ischeck !==scope.row.payType"
                  type="text"
                />
              </span>
            </el-table-column>
            <el-table-column prop="FeeAmount" label="补贴单退款杂费金额">
              <span slot-scope="scope">
                <el-input
                  v-if="scope.row.payType ==1||scope.row.payType ==2||scope.row.payType ==3"
                  v-model="scope.row.FeeAmount"
                  :disabled="ischeck !==scope.row.payType"
                  type="text"
                />
              </span>
            </el-table-column>
            <el-table-column label="操作">
              <span slot-scope="scope">
                <el-button type="text" size="small" @click="refundfun((list.orderSource==1||list.orderSource==2)?list.payDetailsList:scope.row)" v-if="scope.row.payType!=4||(scope.row.payType ==4&&list.useCoin-0)">退款</el-button>
              </span>
            </el-table-column>
          </el-table>
        </el-col>
      </el-row>
    </el-form>
    <!-- 抵扣积分 -->
    <div class="divBar"></div>
    <h5 class="titleInfo">抵扣积分</h5>
    <el-form ref="list" :inline="true" :model="list" label-width="150px">
      <div class="divTable">
        <el-table :data="refundList.dedCreditsInfo" border>
          <el-table-column align="center" prop="applySn" label="订单号"></el-table-column>
          <el-table-column align="center" prop="creditType" label="积分类型"></el-table-column>
          <el-table-column align="center" prop="str" label="原数量"></el-table-column>
          <el-table-column align="center" prop="dedRefund" label="退还数量"></el-table-column>
          <el-table-column align="center" prop="surplus" label="剩余数量"></el-table-column>
        </el-table>
      </div>
    </el-form>
    <!-- 额度 -->
    <div class="divBar"></div>
    <h5 class="titleInfo">额度</h5>
    <el-form ref="list" :inline="true" :model="list" label-width="150px">
      <div class="divTable">
        <el-table :data="data1" border>
          <el-table-column align="center" prop="oldSurplusMoney" label="补贴单原总共"></el-table-column>
          <el-table-column align="center" prop="refundSurplusMoney" label="退款"></el-table-column>
          <el-table-column align="center" prop="surplusMoney" label="剩余"></el-table-column>
          <el-table-column align="center" prop="oldUserQuota" label="已释放"></el-table-column>
          <el-table-column align="center" prop="refundUserQuota" label="退款已释放消费"></el-table-column>
          <el-table-column align="center" prop="userQuota" label="剩余已释放消费"></el-table-column>
          <el-table-column align="center" prop="oldCoin" label="已发放提现"></el-table-column>
          <el-table-column align="center" prop="refundCoin" label="退款已发放提现"></el-table-column>
          <el-table-column align="center" prop="coin" label="剩余已发放提现"></el-table-column>
        </el-table>
      </div>
    </el-form>
    <!-- 消费积分 -->
    <div class="divBar"></div>
    <h5 class="titleInfo">消费积分</h5>
    <div class="divTable">
      <el-table :data="data2" border>
        <el-table-column align="center" prop="oldSurplusCredit" label="补贴单原总共"></el-table-column>
        <el-table-column align="center" prop="refundSurplusCredit" label="退款"></el-table-column>
        <el-table-column align="center" prop="surplusCredit" label="原剩余"></el-table-column>
        <el-table-column align="center" prop="oldUserCredit" label="账户原有"></el-table-column>
        <el-table-column align="center" prop="refundUserCredit" label="退款的"></el-table-column>
        <el-table-column align="center" prop="userCredit" label="剩余的"></el-table-column>
      </el-table>
    </div>
    <!-- 业绩奖励 -->
    <div class="divBar"></div>
    <h5 class="titleInfo">业绩奖励</h5>
    <div class="divTable">
      <el-table :data="refundList.reward" border>
        <el-table-column align="center" prop="awardTypeName" label="奖励类型"></el-table-column>
        <el-table-column align="center" prop="realname" label="姓名"></el-table-column>
        <el-table-column align="center" prop="username" label="电话"></el-table-column>
        <el-table-column align="center" prop="roleName" label="角色"></el-table-column>
        <el-table-column align="center" prop="amount" label="奖励金额"></el-table-column>
        <el-table-column align="center" prop="refundPrice" label="退款金额"></el-table-column>
        <el-table-column align="center" prop="overRefundPrice" label="剩余金额"></el-table-column>
      </el-table>
    </div>
    <el-row v-if="refundList.reward">
      <el-col :span="24">
        <el-button
          style="margin-top:30px;margin-left:130px;margin-bottom:30px"
          type="primary"
          @click="setRefund"
        >立即提交</el-button>
      </el-col>
    </el-row>
  </div>
</template>
<script>
import {
  refund,
  getRefund,
  setRefund,
  setRefund4Combined
} from "@/api/subsidyManage";
export default {
  name: "subsidyManageRefund",
  data() {
    return {
      param: {},
      ischeck: false,
      refundAllMoney: null,
      val: "1",
      userId: "",
      list: {},
      refundList: {},
      feeAmount: "",
      data1: [],
      data2: [],
      tableData: [
        {
          id: "12987122",
          name: "222",
          amount1: "234",
          amount2: "3.2",
          amount3: 10
        },
        {
          id: "12987123",
          name: "1",
          amount1: "165",
          amount2: "4.43",
          amount3: 12
        }
      ]
    };
  },
  filters: {
    filterNull(e) {
      if (!e) {
        return "暂无";
      } else {
        return e;
      }
    },
    cardNull(e) {
      return window.helper.cardType(e);
    },
    userFocus(e) {
      return window.helper.userFocus(e);
    }
  },
  inject: ["reload"],
  mounted() {
    this.userId = String(this.$route.query.id);
    this.getList();
  },
  methods: {
    //计算
    async sumpay() {
      console.log(this.list.payDetailsList,this.list.payTypeInt ,'支付类型');
      let list = this.list.payDetailsList?this.list.payDetailsList:[];
      let AllMoney = this.refundAllMoney;
      let FeeAmount = 0;
      let flat = false;
      let param = {};
      if(this.list.payTypeInt ==4){
            list.map(res => {
        AllMoney += res.AllMoney ? res.AllMoney - 0 : 0;
        FeeAmount += res.FeeAmount ? res.FeeAmount - 0 : 0;
        if (res.payType == 5) {//平台现金支付
          param.platformRefundMoney = res.AllMoney ? res.AllMoney + "" : "";
        }
        if (res.payType == 4) {//金币
          param.refundUseCoin = res.AllMoney ? res.AllMoney + "" : "";
          console.log('param.refundUseCoin',param.refundUseCoin,this.list.useCoin)
          
        }
        if (res.payType == 3) {
          param.onlineRefundAllMoney = res.AllMoney ? res.AllMoney + "" : "";
          param.onlineRefundFeeAmount = res.FeeAmount ? res.FeeAmount + "" : "";
        }
        if (res.payType == 2) {
          param.stageRefundAllMoney = res.AllMoney ? res.AllMoney + "" : "";
          param.stageRefundFeeAmount = res.FeeAmount ? res.FeeAmount + "" : "";
        }
        if (res.payType == 1) {
          param.ownRefundAllMoney = res.AllMoney ? res.AllMoney + "" : "";
          param.ownRefundFeeAmount = res.FeeAmount ? res.FeeAmount + "" : "";
        }
        console.log(res.AllMoney,res.payAllAmount)
        if((res.payType == 4&&res.AllMoney-0 > res.payTotalAmount-0)||(res.payType != 4&&(res.FeeAmount-0 > res.payFeeAmount-0 ||
          res.AllMoney-0 > res.payAllAmount-0))){
          flat = true;
        }
        // else if (
        //   res.FeeAmount-0 > res.payFeeAmount-0 ||
        //   res.AllMoney-0 > res.payAllAmount-0
        // ) {
        //   // this.$message.warning(
        //   //   "录入的退款金额大于流水单对应的付款方式金额，请重新输入"
        //   // );
        //   flat = true;
        // }
      });
      }
    
      if(param.refundUseCoin-0>this.list.useCoin-0){
        this.$message.warning("不可输入大于该补贴单实际扣除金币的值");
        return;
      }
      console.log(AllMoney-param.refundUseCoin)
      if((AllMoney-param.refundUseCoin)-0>this.list.allowanceMoney-0){
        this.$message.warning("补贴单退款奖励金额总和不可大于实际扣除现金金额");
        return;
      }
      // if (AllMoney > this.list.allowanceMoney-0) {
      //   this.$message.warning("退款奖励金额不能大于补贴单奖励金额");
      //   return;
      // }
      if (FeeAmount > list.money - list.allowanceMoney) {
        this.$message.warning("退款杂费金额不能大于补贴单杂费金额");
        return;
      }
      if (flat) {
        this.$message.warning(
          "录入的退款金额大于流水单对应的付款方式金额，请重新输入"
        );
        return;
      }
      console.log(1111, AllMoney, FeeAmount);
      this.param = param;
      this.getRefund(AllMoney, FeeAmount,param.refundUseCoin);
      // let res = await getRefund(param);
      //  if (res.data.code == 0) {
      //     this.refundList = res.data.data;
      //     this.data1 = Array(this.refundList.coinInfo);
      //     this.data2 = Array(this.refundList.creditsInfo);
      //   } else {
      //     console.log(res.data.msg);
      //     this.$message.error(res.data.msg);
      //   }
      // this.$message.warning(res.data.msg);
    },
    refundfun(row) {
      console.log(row)
      console.log(this.list.orderSource);
      this.ischeck = row.payType;
      if(this.list.orderSource==1||this.list.orderSource==2){
        console.log('卡券项目')
        row.forEach(i=>{
          i.AllMoney=i.payTotalAmount
        })
        // row.AllMoney=row.payTotalAmount

        this.ischeck =false
      }
    },
    /** */
    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (column.label.indexOf("操作") > -1) {
          sums[index] = ``;
          return;
        }
        if (index === 0) {
          sums[index] = "总价";
          return;
        }

        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          sums[index] += " 元";
        } else {
          sums[index] = "N/A";
        }
      });

      return sums;
    },

    //退款动作提交
    setRefund() {
      let aaa = {
        id: this.userId,
        goldRefundMoney:this.param.refundUseCoin,
        feeAmount: this.feeAmount,
        refundAllMoney: this.refundAllMoney,
        is2020Refund :this.list.is2020Refundval
      };
      let datas = Object.assign(aaa, this.param);
      if (this.list.payTypeInt !== 4) {
        setRefund(aaa)
          .then(res => {
            if (!res.data.code) {
              this.$router.push("/subsidyManageIndex");
              this.reload();
            } else {
              console.log(res.data.msg);
              this.$message.error(res.data.msg);
            }
          })
          .catch(error => {
            this.$message.error(error);
          });
      } else {
        setRefund4Combined(datas)
          .then(res => {
            if (!res.data.code) {
              this.$router.push("/subsidyManageIndex");
              this.reload();
            } else {
              console.log(res.data.msg);
              this.$message.error(res.data.msg);
            }
          })
          .catch(error => {
            this.$message.error(error);
          });
      }
    },
    //页面计算
    getRefund(AllMoney, feeAmount,refundUseCoin) {
      console.log('计算'+AllMoney, feeAmount,refundUseCoin)
      console.log( this.param.refundUseCoin)
      let datas = {
        id: this.userId,
        refundUseCoin:refundUseCoin,
        refundAllMoney: this.param.refundUseCoin
          ? AllMoney-this.param.refundUseCoin + ""
          : AllMoney+"",
        feeAmount: feeAmount ? feeAmount + "" : this.feeAmount,
         is2020Refund :this.list.is2020Refundval
      };
      console.log(7777, datas);
      getRefund(datas).then(res => {
        if (res.data.code == 0) {
          this.refundList = res.data.data;
          this.data1 = Array(this.refundList.coinInfo);
          this.data2 = Array(this.refundList.creditsInfo);
        } else {
          console.log(res.data.msg);
          this.$message.error(res.data.msg);
        }
      });
    },
    //审批取消
    auditClose() {
      this.dialogAudit = false;
      console.log("审核取消");
    },
    //审批确定
    auditSubmit() {
      this.dialogAudit = false;
      console.log("审批确定");
    },
    getList() {
      let datas = { id: this.userId };
      refund(datas)
        .then()
        .then(res => {
          console.log(res);
          if (!res.data.code) {
            this.list = res.data.data;
          } else {
            this.$message.error(res.data.msg);
          }
        })
        .catch(error => {
          this.$message.error(error);
        });
    }
  }
};
</script>

<style scoped>
.app-container {
  padding: 0px !important;
}
.form {
  margin-left: 268px;
}
.tableData /deep/ tbody.has-gutter td:last-of-type {
  color: #409eff;
}
.divTable {
  padding: 0 20px 20px 20px;
}
</style>